package com.gw.yunke.web;

import com.gw.yunke.domain.Collect;
import com.gw.yunke.domain.User;
import com.gw.yunke.domain.dto.CollectReq;
import com.gw.yunke.domain.dto.CollectResp;
import com.gw.yunke.domain.dto.ResultData;
import com.gw.yunke.service.ICollectService;
import com.gw.yunke.service.IUserService;
import com.gw.yunke.service.impl.CollectServiceImpl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 收藏课程
 */
@RestController
@RequestMapping(value = "/collect")
public class CollectController {
  private static final Logger log = LoggerFactory.getLogger(CollectController.class);

  @Autowired
  private ICollectService collectService;
  @Autowired
  private IUserService userService;

  @RequestMapping(value = "/add",method = RequestMethod.POST)
  public ResultData addCollect(@RequestBody CollectReq collectReq, @CookieValue("sso") String token) throws Exception {
    ResultData resultData = new ResultData();
    try{
      User user = userService.getUserInfoByToken(token);
      if(user == null){
        log.error("[收藏] 未找到用户信息");
        throw new Exception("未登录");
      }
      if(collectReq.getCourseId() == null){
        log.error("[收藏] 参数错误 缺失课程id");
        throw new Exception("缺失课程id");
      }
      Collect collect = new Collect();
      collect.setUserId(user.getId());
      collect.setCourseId(collectReq.getCourseId());
      int result = collectService.addCollect(collect);
      if(result > 0){
        resultData.setCode(ResultData.OK);
      }else{
        resultData.setCode(ResultData.ERROR);
      }
      return resultData;

    } catch (Exception e) {
      e.printStackTrace();
      throw e;
    }
  }

  @RequestMapping(value = "/get",method = RequestMethod.POST)
  public ResultData<List<CollectResp>> getUserCollects(@RequestBody CollectReq collectReq,@CookieValue("sso")String token) throws Exception {
    ResultData resultData = new ResultData();
    try{
      User user = new User();
      userService.checkLogin(token,user);

      List<CollectResp> collectResps = collectService.getCollects(collectReq);
      resultData.setCode(ResultData.OK);
      resultData.setData(collectResps);
      return resultData;

    } catch (Exception e) {
      e.printStackTrace();
      throw e;
    }
  }

  @RequestMapping(value = "/cancel",method = RequestMethod.POST)
  public ResultData cancelCollect(@RequestBody CollectReq collectReq,@CookieValue("sso")String token) throws Exception {
    ResultData resultData = new ResultData();
    try{
      User user = new User();
      userService.checkLogin(token,user);
      int result = collectService.cancelCollect(collectReq.getCourseId());
      if(result > 0)
        resultData.setCode(ResultData.OK);
      else{
        resultData.setCode(ResultData.ERROR);
        resultData.setMessage("未找到对应记录 删除失败");
      }
      return resultData;

    } catch (Exception e) {
      e.printStackTrace();
      throw e;
    }
  }
}
